package cn.ydxlm.paylib.ali;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;

import com.alipay.sdk.app.PayTask;
import cn.ydxlm.paylib.PayCallback;
import cn.ydxlm.paylib.PayConfig;
import cn.ydxlm.paylib.PayResult;
import com.xujl.task.Emitter;
import com.xujl.task.RxExecutor;
import com.xujl.task.Task;

import java.util.Map;

import androidx.annotation.NonNull;

/**
 * 阿里支付
 */
public class AliPay {
    private static final int SDK_PAY_FLAG = 1;
    private static final int SDK_AUTH_FLAG = 2;
    private PayCallback mPayCallback;
    @SuppressLint("HandlerLeak")
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage (@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case SDK_PAY_FLAG: {
                    @SuppressWarnings("unchecked")
                    PayResult payResult = new PayResult((Map<String, String>) msg.obj);
                    /**
                     * 对于支付结果，请商户依赖服务端的异步通知结果。同步通知结果，仅作为支付结束的通知。
                     */
                    String resultInfo = payResult.getResult();// 同步返回需要验证的信息
                    String resultStatus = payResult.getResultStatus();
                    PayConfig.log(payResult.toString());
                    if (mPayCallback == null) {
                        return;
                    }
                    // 判断resultStatus 为9000则代表支付成功
                    if (TextUtils.equals(resultStatus, "9000")) {
                        // 该笔订单是否真实支付成功，需要依赖服务端的异步通知。
                        mPayCallback.onSuccess(payResult);
                    } else {
                        // 该笔订单真实的支付结果，需要依赖服务端的异步通知。
                        mPayCallback.onError(payResult, "支付失败");
                    }
                    break;
                }
                case SDK_AUTH_FLAG: {

                    break;
                }
                default:
                    break;
            }
        }
    };

    public void pay (Activity activity, String orderInfo) {
        RxExecutor.getInstance()
                .executeTask(new Task<Object>() {
                    @Override
                    public void run (Emitter<Object> emitter) throws Exception {
                        super.run(emitter);
                        PayTask alipay = new PayTask(activity);
                        Map<String, String> result = alipay.payV2(orderInfo, true);
                        Message msg = new Message();
                        msg.what = SDK_PAY_FLAG;
                        msg.obj = result;
                        mHandler.sendMessage(msg);
                    }
                });
    }

    public void setPayCallback (PayCallback payCallback) {
        mPayCallback = payCallback;
    }

    public static AliPay getInstance () {
        return Holder.ourInstance;
    }

    private AliPay () {
    }

    private static class Holder {
        private static final AliPay ourInstance = new AliPay();
    }
}
